package com.tensquare.search.controller;


import com.tensquare.search.pojo.Article;
import com.tensquare.search.service.ArticleService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: tensquare_parent
 * @BelongsPackage: com.tensquare.search.controller
 * @Author: yang
 * @CreateTime: 2019-03-07 13:28
 * @Description: ${Description}
 */

@RestController
@CrossOrigin
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;


    /**
     *  添加到索引库
     * @param article
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Article article){
        articleService.save(article);
        return  new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     *  关键字搜索文章
     * @param key
     * @param page
     * @param size
     * @return
     */

    @RequestMapping(value="/{key}/{page}/{size}",method = RequestMethod.GET)
    public Result findByKey(@PathVariable String key,@PathVariable int page,@PathVariable int size){
        Page<Article> pageDate=articleService.findByKey(key,page,size);
        return new Result(true,StatusCode.OK,"查询成功",new PageResult<Article>(pageDate.getTotalElements(),pageDate.getContent()));
    }

}
